import os
import uuid
import base64
import hashlib
import datetime

from app.config.settings import SUPPORT_FILES,RESOURCE_PATH

class Tools:

    @staticmethod
    def uid():
        """
        Uid of type uuid4
        :return:
        """
        return str(uuid.uuid4()).replace('-', '')

    @staticmethod
    def sha1_salt(salt: str):
        """
        Sha1 salt encryption
        :param salt:
        :return:
        """
        sha1 = hashlib.sha1(salt.encode('utf-8'))
        return sha1.hexdigest()

    @staticmethod
    def uuid_name(filename: str):
        """
        The uuid generation rule is salt encryption of dns namespace,and the type is uuid5
        :param filename:
        :return: uuid
        """
        _uid = uuid.uuid5(uuid.NAMESPACE_DNS, filename)
        uid_name = f"{str(_uid).replace('-', '')}"
        return uid_name

    @staticmethod
    def base64_encode(args: str):
        """
        String converted to Base64
        :param string:
        :return:
        """
        result = base64.b64encode(args.encode('utf-8')).decode()
        return result

    @staticmethod
    def base64_decode(args: str):
        """
        Base64 string is restored to regular string
        :param string:
        :return: str
        """
        result = base64.b64decode(args).decode("utf-8")
        return result

    @staticmethod
    def date_format(dt):
        """
        Datetime type is converted to formatted time type
        :param dt: datetime
        :return: str time
        """
        return f"{dt.year}-{dt.month}-{dt.day} {dt.hour}:{str(dt.minute).zfill(2)}:{str(dt.second).zfill(2)}"

    @staticmethod
    def str_time2datetime(str_time):
        return datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')

    @staticmethod
    def number_0_before(number: int, offset: int) -> str:
        """
        Used zfill
        :param number: integer
        :param offset: integer
        :return:
        """
        return str(number).zfill(offset)

    @staticmethod
    def is_number(number:str):
        """校验字符串是否为纯数字"""
        try:
            int(number)
            return True
        except ValueError:
            print("出错辣!!")
            return None

